//
//  PrefixHeader.pch
//  ZDMiaoBo
//
//  Created by 张东 on 16/9/29.
//  Copyright © 2016年 张东. All rights reserved.
//


#import "ZHRTBasicMethod.h"

@implementation ZHRTBasicMethod

+ (NSString*)DataTOjsonString:(id)object
{
    NSString *jsonString = nil;
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
                                                       options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                         error:&error];
    
    if (! jsonData) {
        NSLog(@"Got an error: %@", error);
    } else {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    return jsonString;
}

+ (BOOL)isMobileNumber:(NSString *)mobileNum
{ /**
   * 手机号码
   * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
   * 联通：130,131,132,152,155,156,185,186
   * 电信：133,1349,153,180,189,181(增加)
   */
//    NSString * MOBIL = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188,184
     12         */
//    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[2378])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
//    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189,181(增加)
     22         */
//    NSString * CT = @"^1((33|53|8[0149]|70)[0-9]|349)\\d{7}$";
    NSString * MOBIL = @"^1[3|4|5|7|8]\\d{9}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBIL];
//    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
//    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
//    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
//    if (([regextestmobile evaluateWithObject:mobileNum]
//         || [regextestcm evaluateWithObject:mobileNum]
//         || [regextestct evaluateWithObject:mobileNum]
//         || [regextestcu evaluateWithObject:mobileNum])) {
//        return YES;
//    }
    if ([regextestmobile evaluateWithObject:mobileNum]) {
         return YES;
    }
    return NO;
}

+ (NSString *)ChangeNumberFormat:(NSString *)num
{
    BOOL isPoint = NO;
    NSArray *array = [num componentsSeparatedByString:@"."];
    
    if (num == nil) {
        return num;
    }
    // NSMutableString * mstr = [[NSMutableString alloc] init];
    for (int i = 0; i < num.length; i++) {
        unichar ch = [num characterAtIndex:i];
        if (ch == '.') {
            isPoint = YES;
            num = array.firstObject;
        }
    }
    int count = 0;
    long long int a = num.longLongValue;
    while (a != 0)
    {
        count++;
        a /= 10;
    }
    NSMutableString *string = [NSMutableString stringWithString:num];
    NSMutableString *newstring = [NSMutableString string];
    while (count > 3) {
        count -= 3;
        NSRange rang = NSMakeRange(string.length - 3, 3);
        NSString *str = [string substringWithRange:rang];
        [newstring insertString:str atIndex:0];
        [newstring insertString:@"," atIndex:0];
        [string deleteCharactersInRange:rang];
    }
    [newstring insertString:string atIndex:0];
    if (isPoint) {
        NSMutableString * str2 = [[NSMutableString alloc] initWithString:array.lastObject];
        
        NSString * ss;
        if (str2.length >= 2) {
            ss = [str2 substringToIndex:2];
        }else {
            ss = array.lastObject;
        }
        
        
        return [NSString stringWithFormat:@"%@.%@",newstring,ss];
    }
    
    return [NSString stringWithFormat:@"%@.00",newstring] ;
}
+ (CGRect)createFrame:(CGRect)frame {
    
    float coefficient = kScreenH / 568;
    CGSize  size = frame.size;
    CGPoint point = frame.origin;
    
    point.y *= coefficient;
    size.height *= coefficient;
    frame.size = size;
    frame.origin = point;

    return frame;
}
// 判断密码是否同时包含数字和字符
+(BOOL)judgePassWordLegal:(NSString *)pass{
    BOOL result = false;
//    if ([pass length] >= 8){
        // 判断长度大于8位后再接着判断是否同时包含数字和字符
        NSString * regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        result = [pred evaluateWithObject:pass];
//    }
    
    return result;
}


+ (BOOL)checkIdentityCardNo:(NSString*)cardNo

{
    
    if (cardNo.length != 18) {
        //        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"对不起!省份证的位数不够或过多" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        //
        //        [alert show];
        //
        return  NO;
        
    }
    
    NSArray* codeArray = [NSArray arrayWithObjects:@"7",@"9",@"10",@"5",@"8",@"4",@"2",@"1",@"6",@"3",@"7",@"9",@"10",@"5",@"8",@"4",@"2", nil];
    
    NSDictionary* checkCodeDic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1",@"0",@"X",@"9",@"8",@"7",@"6",@"5",@"4",@"3",@"2", nil]  forKeys:[NSArray arrayWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil]];
    
    
    
    NSScanner* scan = [NSScanner scannerWithString:[cardNo substringToIndex:17]];
    
    
    
    int val;
    
    BOOL isNum = [scan scanInt:&val] && [scan isAtEnd];
    
    if (!isNum) {
        //        NSLog(@"输入的省份证号码不对");
        return NO;
    }
    
    int sumValue = 0;
    
    for (int i =0; i<17; i++) {
        
        sumValue+=[[cardNo substringWithRange:NSMakeRange(i , 1) ] intValue]* [[codeArray objectAtIndex:i] intValue];
        
    }
    

    NSString* strlast = [checkCodeDic objectForKey:[NSString stringWithFormat:@"%d",sumValue%11]];
    
    
    
    if ([strlast isEqualToString: [[cardNo substringWithRange:NSMakeRange(17, 1)]uppercaseString]]) {
        //        NSLog(@"验证省份证号码可用");
        return YES;
        
    }
    //    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"省份证份证号码错误" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    //    
    //    [alert show];
    return  NO;
    
}

// 中文输入
+ (BOOL)isChinese: (NSString *)str {
     BOOL result = false;
    NSString *regex = @"^[\u4E00-\u9FFF]+$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    result = [pred evaluateWithObject:str];
    return result;
}

+ (NSString *)effectiveFigure:(double)oldValue {
    
    double a = oldValue * 100;
    float b = a / 10;
    int c = a / 10;
    float d = a / 100;
    int f = a / 100;
    if (d == f) {
        return [NSString stringWithFormat:@"%zd",(int)oldValue];
    }
    if (b == c) {
        return [NSString stringWithFormat:@"%.1f",oldValue];
    }
    return [NSString stringWithFormat:@"%.2f",oldValue];
}


@end
